package chan.project.service.impl;

import chan.project.constant.LocationConstant;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import chan.project.common.ErrorCode;
import chan.project.constant.CommonConstant;
import chan.project.exception.ThrowUtils;
import chan.project.mapper.LocationMapper;
import chan.project.model.dto.location.LocationQueryRequest;
import chan.project.model.entity.Location;
import chan.project.model.entity.User;
import chan.project.model.vo.LocationVO;
import chan.project.model.vo.UserVO;
import chan.project.service.LocationService;
import chan.project.service.UserService;
import chan.project.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 地点服务实现
 *
 * @author <a href="https://github.com/kavansp">kavansp</a>
 * 
 */
@Service
@Slf4j
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {


    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param location
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validLocation(Location location, boolean add) {
        ThrowUtils.throwIf(location == null, ErrorCode.PARAMS_ERROR);
        Long id = location.getId();
        String name = location.getName();
        String coordinates = location.getCoordinates();
        String description = location.getDescription();
        // 创建数据时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(name), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(description),ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        ThrowUtils.throwIf(StringUtils.isBlank(name), ErrorCode.PARAMS_ERROR);
        if (StringUtils.isNotBlank(description)) {
            ThrowUtils.throwIf(description.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
        }
    }

    /**
     * 获取查询条件
     *
     * @param locationQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Location> getQueryWrapper(LocationQueryRequest locationQueryRequest) {
        QueryWrapper<Location> queryWrapper = new QueryWrapper<>();
        if (locationQueryRequest == null) {
            return queryWrapper;
        }
        Long id = locationQueryRequest.getId();
        String name = locationQueryRequest.getName();
        String description = locationQueryRequest.getDescription();
        String searchText = locationQueryRequest.getSearchText();
        String sortField = locationQueryRequest.getSortField();
        String sortOrder = locationQueryRequest.getSortOrder();

        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("name", searchText).or().like("description", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
//         精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
//         排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取地点封装
     *
     * @param location
     * @param request
     * @return
     */
    @Override
    public LocationVO getLocationVO(Location location, HttpServletRequest request) {
        // 对象转封装类
        LocationVO locationVO = LocationVO.objToVo(location);
        return locationVO;
    }

    /**
     * 分页获取地点封装
     *
     * @param locationPage
     * @param request
     * @return
     */
    @Override
    public Page<LocationVO> getLocationVOPage(Page<Location> locationPage, HttpServletRequest request) {
        List<Location> locationList = locationPage.getRecords();
        Page<LocationVO> locationVOPage = new Page<>(locationPage.getCurrent(), locationPage.getSize(), locationPage.getTotal());
        if (CollUtil.isEmpty(locationList)) {
            return locationVOPage;
        }
        // 对象列表 => 封装对象列表
        List<LocationVO> locationVOList = locationList.stream().map(location -> {
            return LocationVO.objToVo(location);
        }).collect(Collectors.toList());
        locationVOPage.setRecords(locationVOList);
        return locationVOPage;
    }
}
